package com.kkm.controller;

import com.kkm.pojo.Brand;
import com.kkm.pojo.PageBean;
import com.kkm.pojo.Type;
import com.kkm.service.BrandService;
import com.kkm.service.Imp.BrandServiceImp;
import com.kkm.service.Imp.TypeServiceImp;
import com.kkm.service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/brand")
public class BrandServlet extends BaseServlet{
    private BrandService service=new BrandServiceImp();

    //展示分类
    public void showBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=5;
        req.removeAttribute("currPageMax");
        PageBean<Brand> pageBeanBrand=service.showBrand(currentPage,rows);
        req.setAttribute("Brand",pageBeanBrand);
        req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
    }

    //添加类型
    public void addBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bname=req.getParameter("brandName");
        service.addBrand(bname);
        //更新新的列表数据
        String currentPage=req.getParameter("currentPage");
        int rows=5;
        PageBean<Brand> pageBeanBrand=service.showBrand(currentPage,rows);
        req.setAttribute("Brand",pageBeanBrand);
        req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
    }
    //模糊查询类型
    public void checkBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String checkName=req.getParameter("checkName");
        String currentPage=req.getParameter("currentPage");
        int rows=5;
        PageBean<Brand> brandList=service.checkBrand(checkName,currentPage,rows);
        if(brandList==null){
            brandList=service.showBrand(currentPage,rows);
            req.setAttribute("Brand",brandList);
            req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
        }else {
            req.setAttribute("Brand",brandList);
            req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
        }

    }
    //删除分类
    public void deleteBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tid =req.getParameter("tid");
        service.deleteBrand(tid);

        String currentPage=req.getParameter("currentPage");
        int rows=5;
        req.removeAttribute("currPageMax");
        PageBean<Brand> pageBeanBrand=service.showBrand(currentPage,rows);
        req.setAttribute("Brand",pageBeanBrand);
        req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
    }
    //修改分类名称
    public void modifyBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String newName=req.getParameter("modBrandName");
        String bid =req.getParameter("bid");
        service.modifyBrand(bid,newName);

        String currentPage=req.getParameter("currentPage");
        int rows=5;
        req.removeAttribute("currPageMax");
        PageBean<Brand> pageBeanBrand=service.showBrand(currentPage,rows);
        req.setAttribute("Brand",pageBeanBrand);
        req.getRequestDispatcher("/showBrand.jsp").forward(req,resp);
    }
}
